<template>
	<view class="">
		<view class="filecen" v-for="(item,index) in mate" :key="index">
			<view class="filecen_data">
				<u-icon size="30" name="file-text"></u-icon>
				<view class="filecen_data_cen">{{item.name}}</view>
				<view class="" @click="download(index)">
					<u-icon size="30" name="download"></u-icon>
				</view>
			</view>
		</view>
		
		<u-empty mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png" text='暂无数据'
			v-if="!mate.length"></u-empty>
	</view>


</template>

<script>
	import {
		api_getcolumndownfile
	} from '@/api/product.js'
	export default {
		data() {
			return {
				mate: []
			}
		},
		onLoad(option) {
			this.option = option;
			this.query();
		},
		methods: {
			async query() {
				let res = await api_getcolumndownfile({
					column_id: this.option.id,
					shop_name: "db"
				});
				if (res.code) {
					this.mate = res.data.list;
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'error'
					});
				}
			},
			
			download(i) { //这个i是上面带的索引位
			
			const tempLink = document.createElement("a");
			  tempLink.style.display = "none";
			  tempLink.href = this.mate[i].src;
			  tempLink.setAttribute("download", this.mate[i].name);
			  tempLink.setAttribute("target", "_blank");
			  document.body.appendChild(tempLink);
			  tempLink.click();
			  document.body.removeChild(tempLink);
		
			
			
			
			
			
				// uni.downloadFile({
				// 	url: this.mate[i].src, //下载地址，后端接口获取的链接
				// 	success: (data) => {
				// 		if (data.statusCode === 200) {
				// 			uni.saveFile({ //文件保存到本地
				// 				tempFilePath: data.tempFilePath, //临时路径
				// 				success: function(res) {
				// 					uni.showToast({
				// 						icon: 'none',
				// 						mask: true,
				// 						title: '文件已保存：' + res.savedFilePath, //保存路径
				// 						duration: 3000,
				// 					});
				// 					setTimeout(() => { //可要可不要 这里测试一下是否下载成功
				// 						//打开文档查看
				// 						uni.openDocument({
				// 							filePath: res.savedFilePath,
				// 							success: function(res) {
				// 								console.log('打开文档成功');
				// 							}
				// 						});
				// 					}, 2000)
				// 				}
				// 			});
				// 		}
				// 	},
				// 	fail: (err) => {
				// 		console.log(err);
				// 		uni.showToast({
				// 			icon: 'none',
				// 			mask: true,
				// 			title: '失败请重新下载',
				// 		});
				// 	},
				// });
			},
		}
	}
</script>

<style lang="scss" scoped>
	.filecen_data {
		width: 100%;
		padding: 20rpx 20rpx;
		display: flex;
		align-items: center;
		height: 90rpx;

		.filecen_data_cen {
			font-size: 30rpx;
			margin-left: 20rpx;
			flex: 1;
		}
	}

	.filecen {
		width: 100%;
		display: flex;
		justify-content: center;
		align-items: center;

		.file {
			width: 140rpx;
			height: 140rpx;
			text-align: center;
			line-height: 140rpx;
			border: 1rpx solid #CCCCCC;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}
</style>